﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace StringMatch
{
    class Program
    {
        static void Main(string[] args)
        {

            #region Code to generate the random string
            /*
            Random rng = new Random();
            int a = 0, b = 0, c = 0, d = 0, rand;
            StringBuilder text = new StringBuilder();
            //Randomly generate 100k uniformly distributed characters from {a, b, c, d}
            for (int i = 0; i < 100000; i++)
            {
                rand = rng.Next(0, 4);
                switch (rand)
                {
                    case 0:
                        a++;
                        text.Append("a");
                        break;
                    case 1:
                        b++;
                        text.Append("b");
                        break;
                    case 2:
                        c++;
                        text.Append("c");
                        break;
                    case 3:
                        d++;
                        text.Append("d");
                        break;
                    default:
                        Console.WriteLine("Error: rng spit out {0}", rand);
                        break;
                }
            }

            using (StreamWriter outfile = File.CreateText(MatchTester.textFile))
            {
                outfile.Write(text.ToString());
            }
            */
            #endregion Code to generate the random string

            //The random string is located in the working directory
            #region Code to generate the answers
            /*
            string text;
            using (StreamReader infile = File.OpenText(MatchTester.textFile))
            {
                text = infile.ReadToEnd();
            }

            List<int> p1Matches = new List<int>();
            List<int> p2Matches = new List<int>();
            List<int> p3Matches = new List<int>();

            #region fill p1Matches
            int index = -1;
            while(true)
            {
                //Start index at -1, start the search at index+1 so the previous match is not counted again
                index = text.IndexOf(MatchTester._p1, index+1);
                if (index == -1)
                    //-1 means no match was found so break
                    break;
                else
                    //Add the index of the match to the list
                    p1Matches.Add(index);
            }
            #endregion fill p1Matches
            #region fill p2Matches
            index = -1;
            while (true)
            {
                index = text.IndexOf(MatchTester._p2, index+1);
                if (index == -1)
                    break;
                else
                    p2Matches.Add(index);
            }
            #endregion fill p2Matches
            #region fill p3Matches
            index = -1;
            while (true)
            {
                index = text.IndexOf(MatchTester._p3, index+1);
                if (index == -1)
                    break;
                else
                    p3Matches.Add(index);
            }
            #endregion fill p3Matches

            //Put these in the debug bin in their own file
            using (StreamWriter outfile1 = File.CreateText(MatchTester.p1File))
            {
                for (int i = 0; i < p1Matches.Count(); i++)
                    outfile1.WriteLine(p1Matches[i]);
            }
            using (StreamWriter outfile2 = File.CreateText(MatchTester.p2File))
            {
                for (int i = 0; i < p2Matches.Count(); i++)
                    outfile2.WriteLine(p2Matches[i]);
            }
            using (StreamWriter outfile3 = File.CreateText(MatchTester.p3File))
            {
                for (int i = 0; i < p3Matches.Count(); i++)
                    outfile3.WriteLine(p3Matches[i]);
            }
            */
            #endregion Code to generate the answers

            MatchTester mt = new MatchTester();

            //bool answer = mt.TestAlgorithm(new MatchTester.Matcher(Matcher));
            bool answer = mt.TestAlgorithm(new MatchTester.Matcher(StringMatch.MatchAlgorithms.FiniteAutomaton.Match));
            if (answer)
                Console.Write("It worked!");
            else
                Console.Write("No beans");

            Console.Read();
        }

        //Matcher to test the match tester
        public static List<int> Matcher(string pattern, string text)
        {
            List<int> result = new List<int>();

            int index = -1;
            while (true)
            {
                //Start index at -1, start the search at index+1 so the previous match is not counted again
                index = text.IndexOf(pattern, index + 1);
                if (index == -1)
                    //-1 means no match was found so break
                    break;
                else
                    //Add the index of the match to the list
                    result.Add(index);
            }

            return result;
        }

    }
}
